const config = require('./config')
const utils = require('./utils')
const tronWeb = require('./tronweb')
const fs = require('fs')
const path = require('path')

const CONTRACT_ID = config.contractId
const STATS_FILE = path.join(config.dataDir, 'stats.json')

function toTrx(sun) {
  return tronWeb.fromSun(sun)
}

// global variables
let contract
let gameConfigure

async function pullStats() {
  console.log('======================begin pull stats=====================')
  let r = await tronWeb.trx.getAccountResources(CONTRACT_ID)
  let energyUsed = Number(r.EnergyUsed) || 0
  let energyLimit = Number(r.EnergyLimit) || 0
  console.log('get account resource', r)
  let gameInfo = await contract.getGameInfo().call()
  let balance = tronWeb.fromSun(gameInfo[0].toString())
  let marketValue = tronWeb.fromSun(gameInfo[1].toString())
  let grossIncome = tronWeb.fromSun(gameInfo[2].toString())
  let bonusIndex = tronWeb.fromSun(gameInfo[3].toString())
  let pixels = gameInfo[4]
  let pixelsPainted = Number(gameInfo[5].toString())
  let lastPaintedAt = gameInfo[6].toString()
  let lastPainter = tronWeb.address.fromHex(gameInfo[7].toString())
  let endTime = gameInfo[8].toString()
  let lastStageIncome = toTrx(gameInfo[9].toString())
  let teamBalance = toTrx(gameInfo[10].toString())
  let stage = gameInfo[11]
  let potAmount = toTrx(gameInfo[12].toString())
  console.log('get game info:', gameInfo)
  r = await Promise.all([
    contract.lastStageIncome().call(),
    contract.teamBalance().call(),
  ])
  console.log('get other contract members:', r)
  const stats = {
    energyUsed,
    energyLimit,
    balance,
    marketValue,
    grossIncome,
    bonusIndex,
    pixels,
    pixelsPainted,
    lastPaintedAt,
    lastPainter,
    endTime,
    stage,
    potAmount,

    lastStageIncome,
    teamBalance,
    gameConfigure,
  }
  fs.writeFileSync(STATS_FILE, utils.prettyJson(stats))
}

async function main() {
  contract = await tronWeb.contract().at(CONTRACT_ID)
  let results = await Promise.all([
    contract.canvasWidth().call(),
    contract.canvasHeight().call(),
    contract.activatePixelThreshold().call(),
    contract.lastStageIncomeThreshold().call(),
    contract.lastStageDuratio().call(),
    contract.defaultPrice().call(),
    contract.incrementRate().call(),
    contract.bonusRatio().call(),
    contract.potRatio().call(),
    contract.referrerRatio().call(),
    contract.feeRatio().call(),
  ])
  gameConfigure = {
    canvasWidth: results[0],
    canvasHeight: results[1],
    activatePixelThreshold: results[2],
    lastStageIncomeThreshold: toTrx(results[3].toString()),
    lastStageDuratio: results[4],
    defaultPrice: toTrx(results[5].toString()),
    incrementRate: results[6],
    bonusRatio: results[7],
    potRatio: results[8],
    referrerRatio: results[9],
    feeRatio: results[10],
  }
  console.log('start stats service ...')
  console.log('gameConfigure:', gameConfigure, results)
  utils.loopAsync(pullStats, 3000)
}

main().then().catch(console.log)